#!/usr/bin/python
# -*- coding: utf-8 -*-

from opends import OpenDS

__author__ = 'xiwei'


class DS:
    def __init__(self, domain, username, password):
        self.ds = OpenDS()
        self.token = self.ds.ds_login(domain, username, password)['access_token']

    def databases(self):
        res = self.ds.ds_list(self.token).get('data_source', [])
        dbs = {
            ds['name']: {
                tb[0]: {'tb_id': tb[1]} for tb in ds.get('tables')
            } for ds in res
        }
        return dbs

    def tb_info(self, tb_id):
        res = self.ds.field_list(self.token, tb_id)
        return res

    def bulk_delete(self, tb_id, where):
        success = False
        try:
            self.ds.data_bulkdelete(self.token, tb_id, where)
            self.ds.tb_commit(self.token, tb_id)
            success = True
        except Exception, e:
            return success, str(e)
        finally:
            return success, success
